package cn.hahah.module.blog.controller.admin.articletype;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.articletype.vo.*;
import cn.hahah.module.blog.dal.dataobject.articletype.ArticleTypeDO;
import cn.hahah.module.blog.service.articletype.ArticleTypeService;

@Tag(name = "管理后台 - 文章分类")
@RestController
@RequestMapping("/blog/article-type")
@Validated
public class ArticleTypeController {

    @Resource
    private ArticleTypeService articleTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建文章分类")
    @PreAuthorize("@ss.hasPermission('blog:article-type:create')")
    public CommonResult<Long> createArticleType(@Valid @RequestBody ArticleTypeSaveReqVO createReqVO) {
        return success(articleTypeService.createArticleType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章分类")
    @PreAuthorize("@ss.hasPermission('blog:article-type:update')")
    public CommonResult<Boolean> updateArticleType(@Valid @RequestBody ArticleTypeSaveReqVO updateReqVO) {
        articleTypeService.updateArticleType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:article-type:delete')")
    public CommonResult<Boolean> deleteArticleType(@RequestParam("id") Long id) {
        articleTypeService.deleteArticleType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:article-type:query')")
    public CommonResult<ArticleTypeRespVO> getArticleType(@RequestParam("id") Long id) {
        ArticleTypeDO articleType = articleTypeService.getArticleType(id);
        return success(BeanUtils.toBean(articleType, ArticleTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章分类分页")
    @PreAuthorize("@ss.hasPermission('blog:article-type:query')")
    public CommonResult<PageResult<ArticleTypeRespVO>> getArticleTypePage(@Valid ArticleTypePageReqVO pageReqVO) {
        PageResult<ArticleTypeDO> pageResult = articleTypeService.getArticleTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ArticleTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章分类 Excel")
    @PreAuthorize("@ss.hasPermission('blog:article-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleTypeExcel(@Valid ArticleTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ArticleTypeDO> list = articleTypeService.getArticleTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章分类.xls", "数据", ArticleTypeRespVO.class,
                        BeanUtils.toBean(list, ArticleTypeRespVO.class));
    }

}